using System;
using System.ComponentModel;
using System.Windows.Input;
using Giraffe.Client.Common;
using Giraffe.Core.Common.Messengers;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace Giraffe.Client.MainWindowModule.ClientsPanel
{
    public class MessengerStatusViewModel: ViewModelBase
    {
        private readonly IMessenger _messenger;
        private DelegateCommand<object> _loginCommand;
        private DelegateCommand<object> _logoutCommand;

        public MessengerStatusViewModel(IMessenger messenger)
        {
            this._messenger = messenger;
            messenger.Logined += OnLoginChanged;
        }

        private void OnLoginChanged()
        {
            RisePropertyChanged("IsLogin");
        }

        public bool IsLogin
        {
            get
            {
                return _messenger.IsLogined;
            }
        }

        public ICommand LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new DelegateCommand<object>(LoginCommandExecute);
                }
                return _loginCommand;
            }
        }

        public ICommand LogoutCommand
        {
            get
            {
                if (_logoutCommand == null)
                {
                    _logoutCommand = new DelegateCommand<object>(LogoutCommandExecute);
                }
                return _logoutCommand;
            }
        }


        private void LoginCommandExecute(object obj)
        {
            _messenger.Login();
        }

       
        private void LogoutCommandExecute(object obj)
        {
            _messenger.Logout();
        }
    }
}